package com.baosight.sczx.zm.ch.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cg.DailyReport;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlanCM;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ch.domain.ZMCHTSPGDispatch;

/**
 * 竖井提升和排矸情况(调度日报用)
 * <p>Title: ServiceZMCG73.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-27 上午08:01:23  严洪浩  Create
 *</pre>
 */
public class ServiceZMCH40 extends ServiceEPBase {
	/**
	 * 初始进入方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG73</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:37:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG73</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:47:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		String fdMiningId = (String)UserSession.getInSessionProperty("miningCode");
	    String fdDepartmentId = (String)UserSession.getInSessionProperty("orgCode");
	    
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		StaticTools.setQueryParameters(inInfo, "fdReportDepartmentId", fdDepartmentId, true);
		
		EiInfo outInfo = super.query(inInfo, "ZMCHTSPGDispatch.query", new ZMCHTSPGDispatch());
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.status", new String[]{"草拟", "上报"}, new String[]{"草拟", "上报"}, true));//状态
		String tempDate = StringUtils.isNotBlank(inInfo.getString("result-0-fdCurrentDate")) ? inInfo.getString("result-0-fdCurrentDate") : StaticTools.getDate();
		outInfo.addBlock(ProducePlanCM.getWorkingFaceBlock("cmes.cg.workingface", fdMiningId, StaticTools.getYearStr(tempDate), null, null, true));
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", fdMiningId, true));//班次
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		
		return outInfo;
	}

	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25上午11:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCHTSPGDispatch _dom = new ZMCHTSPGDispatch();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			//校验是否重复
	        boolean validate = validate(inInfo, _dom, "ZMCHTSPGDispatch.validate", null);
	        if (validate) {
				out = this.query(inInfo);
				return out;
			} else {
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_TSPGDISPATCH"));
				_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
				_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
				
				_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());
				_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
			    this.dao.insert("ZMCHTSPGDispatch.insert", _dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
				
	}

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-24上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo();
		ZMCHTSPGDispatch _dom = new ZMCHTSPGDispatch();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

	        boolean validate = validate(inInfo, _dom, "ZMCHTSPGDispatch.validate", params);
	        if (validate) {
				out = this.query(inInfo);
				return out;
	        }
	        else {
	        	
	        }
			_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));

		    this.dao.update("ZMCHTSPGDispatch.update", _dom);			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-26上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		EiInfo _temp = super.delete(inInfo, "ZMCHTSPGDispatch.delete");
		
		EiInfo out = new EiInfo();
		out = this.query(inInfo);
		out.setStatus(_temp.getStatus());
		out.setMsg(_temp.getMsg());
		
		return out;	
	}
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCHTSPGDispatch _dom = new ZMCHTSPGDispatch();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> map = new HashMap<String, String>();
			map.put("fdCurrentDate", _dom.getFdCurrentDate());
			map.put("fdWorknumId", _dom.getFdWorknumId());
			
			boolean validate = DailyReport.validate("ZMCGTSPGDispatchFinal.query", map);
			if(validate) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("已经存在数据，无需上报！");
				return out; 
			}
			else {
				int result = dao.update("ZMCHTSPGDispatch.report",_dom);
				if(result > 0)
					DailyReport.copy("ZMCGTSPGDispatchFinal.insert", _dom.toMap());
			}
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}
	
	
	/**
	 * 验证上报是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:04:59 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCHTSPGDispatch _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}

		params.put("fdWorknumId", _dom.getFdWorknumId());
		params.put("fdCurrentDate", _dom.getFdCurrentDate());
		params.put("fdMiningName", _dom.getFdMiningName());
		
		List result = dao.query("ZMCHTSPGDispatch.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "每矿每天只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
	
}
